package han.message;

import com.han.http.client.method.HttpPost;
import com.han.http.client.paramter.FormDataPair;
import com.han.http.client.paramter.RequestHeader;
import com.han.http.fetch.Fetcher;
import han.entity.ContentEntity;
import han.entity.FontEntity;
import han.entity.FriendEntity;
import han.parser.FriendParser;
import han.proof.SmartqqProof;

import java.util.List;

/**
 * @author: Han
 * @date: 2018/7/11 13:47
 * @dscription: 好友管理器, 获取好友信息, 向好友发送信息
 */
public class FriendManager {

    /**
     * 向指定好友发送信息
     *
     * @param content 内容
     * @throws Exception 抛出异常
     */
    public static void sendToFriend(long uin, String content) throws Exception {

        String urlStr = "https://d1.web2.qq.com/channel/send_buddy_msg2";
        SmartqqProof smartqqProof = SmartqqProof.getInstance();
        String cookie = smartqqProof.getCookie().replaceAll("; uin=[o]?", "; uin=o");
        String psessionid = smartqqProof.getPsessionid();
        RequestHeader[] requestHeaders = {
                new RequestHeader("Host", "d1.web2.qq.com"),
                new RequestHeader("Connection", "keep-alive"),
                new RequestHeader("Content-Length", "586"),
                new RequestHeader("Origin", "https://d1.web2.qq.com"),
                new RequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"),
                new RequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk"),
                new RequestHeader("Accept", "*/*"),
                new RequestHeader("Referer", "https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1"),
                new RequestHeader("Accept-Encoding", "gzip, deflate,gbk"),
                new RequestHeader("Accept-Language", "zh-CN,zh;q=0.9"),
                new RequestHeader("Cookie", cookie)
        };
        ContentEntity contentEntity = new ContentEntity(new FontEntity(), content);
        FormDataPair[] formDataPairs = {
                new FormDataPair("r", "{\"to\":" + uin + ",\"content\":" + contentEntity + "\",\"face\":555," +
                        "\"clientid\":53999199,\"msg_id\":8680005,\"psessionid\":\"" + psessionid + "\"}")
        };
        int generalTimeout = 1000 * 10;
        Fetcher fetcher = new Fetcher();
        fetcher.getPageInfoByPost(urlStr, requestHeaders, formDataPairs, generalTimeout, generalTimeout,
                false, HttpPost.UTF_8, null);
    }

    public static void getSelfInfo() throws Exception {
        String urlStr = "https://s.web2.qq.com/api/get_self_info2?t=" + System.currentTimeMillis();
        SmartqqProof smartqqProof = SmartqqProof.getInstance();
        String cookie = smartqqProof.getCookie().replaceAll("; uin=[o]?", "; uin=o");
        RequestHeader[] requestHeaders = {
                new RequestHeader("Host", "s.web2.qq.com"),
                new RequestHeader("Connection", "keep-alive"),
                new RequestHeader("Content-Type", "utf-8"),
                new RequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"),
                new RequestHeader("Accept", "*/*"),
                new RequestHeader("Referer", "https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1"),
                new RequestHeader("Accept-Encoding", "gzip, deflate"),
                new RequestHeader("Accept-Language", "zh-CN,zh;q=0.9"),
                new RequestHeader("Cookie", cookie)
        };
        Fetcher fetcher = new Fetcher();
        int generalTimeout = 1000 * 10;
        String page = fetcher.getPageInfoByGet(urlStr, requestHeaders, generalTimeout, generalTimeout, false, null);
        FriendParser.selfInfo(page);
    }

    /**
     * 获取好友列表(只有昵称和id)
     *
     * @return 好友列表
     * @throws Exception 抛出异常
     */
    public List<FriendEntity> getFriendList() throws Exception {

        String urlStr = "https://s.web2.qq.com/api/get_user_friends2";
        SmartqqProof smartqqProof = SmartqqProof.getInstance();
        String cookie = smartqqProof.getCookie();
        String vfwebqq = smartqqProof.getVfwebqq();
        String hash = smartqqProof.getHash();
        RequestHeader[] requestHeaders = {
                new RequestHeader("Host", "s.web2.qq.com"),
                new RequestHeader("Connection", "keep-alive"),
                new RequestHeader("Content-Length", "148"),
                new RequestHeader("Origin", "https://s.web2.qq.com"),
                new RequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"),
                new RequestHeader("Content-Type", "application/x-www-form-urlencoded"),
                new RequestHeader("Accept", "*/*"),
                new RequestHeader("Referer", "https://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1"),
                new RequestHeader("Accept-Encoding", "gzip, deflate"),
                new RequestHeader("Accept-Language", "zh-CN,zh;q=0.9"),
                new RequestHeader("Cookie", cookie)
        };
        FormDataPair[] formDataPairs = {
                new FormDataPair("r",
                        "{\"vfwebqq\":\"" + vfwebqq + "\",\"hash\":\"" + hash + "\"}")
        };
        int generalTimeout = 1000 * 10;
        Fetcher fetcher = new Fetcher();
        String friendJsonStr = fetcher.getPageInfoByPost(urlStr, requestHeaders, formDataPairs, generalTimeout,
                generalTimeout, false, HttpPost.UTF_8, null);

        return FriendParser.getFriendList(friendJsonStr);
    }
}
